﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Data.Common;
using ZLCMS.Data.Model;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Business.Util;
using Newtonsoft.Json;
using ZLCMS.Business.UploadFileInfo;

namespace ZLCMS.Web.SystemManage.Basics.Shop.Payment
{
    public partial class PaymentDetail : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected string _userAccount;//用户

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();
            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                SaveDoc();
            }
        }

        protected void InitializeGlobalValue()
        {

        }

        protected void InitializeContent()
        {
            //页面控件值
            if (!IsPostBack)
            {

                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    try
                    {
                        string uid = Request["uid"];

                        if (!string.IsNullOrEmpty(uid))
                        {
                            doc_id.Value = uid;

                            ShopPaymentInfo payment = session.Get<ShopPaymentInfo>(Int32.Parse(Request["uid"]));

                            txtTitle.Text = payment.InterfaceId;
                            txtName.Text = payment.PaymentName;
                            txtAcc.Text = payment.SellerEmail;
                            txtAppId.Text = payment.UserId;
                            txtAppKey.Text = payment.PrivateKey;
                            txtKind.Text = payment.Kind;
                            txtPayType.Text = payment.PayType;
                            txtSummary.Value = payment.Description;

                            tb_order.Text = payment.Sort.ToString();
                        }
                    }
                    catch (System.Exception ex)
                    {
                        _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    }
                }
            }
        }


        /// <summary>
        /// 保存数据库
        /// </summary>
        protected void SaveDoc()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                string uid = doc_id.Value;

                ShopPaymentInfo payment;
                if (!string.IsNullOrEmpty(uid))
                {
                    payment = session.Get<ShopPaymentInfo>(Int32.Parse(uid));
                }
                else
                {
                    payment = new ShopPaymentInfo();
                }

                payment.InterfaceId = txtTitle.Text;
                payment.PaymentName = txtName.Text;
                payment.SellerEmail = txtAcc.Text;
                payment.UserId = txtAppId.Text;
                payment.PrivateKey = txtAppKey.Text;
                payment.Kind = txtKind.Text;
                payment.PayType = txtPayType.Text;
                payment.Description = txtSummary.Value;
                payment.Status = "1";
                payment.OwnerAccount = _userAccount;
                session.SaveOrUpdate(payment);
                session.Flush();

                doc_id.Value = payment.Id.ToString();
                ReturnCode rc = new ReturnCode()
                {
                    statusCode = 200,
                    message = "操作成功!"
                };


                rc.callbackType = "forward";
                rc.forwardUrl = "/SystemManage/Basics/Shop/Payment/PaymentDetail.aspx?tabid=paymentdetail";

                if (button_id.Value != "1")
                {
                    rc.forwardUrl += "&uid=" + payment.Id;
                }

                _framePage.Alert(rc);
            }
        }
    }
}
